package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.adapter.SelectGoodsAdapter;
import com.wwwarehouse.contract.bean.ResourceItemBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * @author Joseph on 2017/10/18.
 *         <p/>
 *         商品列表
 */

public class ReleaseGoodsListFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    private ArrayList<ResourceItemBean.DataBean.ListBean> tagBeanList = new ArrayList<>();
    SelectGoodsAdapter mAdapter;
    private int num;
    private int pos;
    private String mSort;
    private String buId;
    private List<ResourceItemBean.DataBean.ListBean> selectTag;

    @Override
    protected int getContentId() {
        return R.layout.release_object_list;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        if (b == null)
            return;
        pos = b.getInt("pos");
        num = b.getInt("num");
        mSort = b.getString("sort");
        buId = b.getString("buId");
        selectTag = (List<ResourceItemBean.DataBean.ListBean>) b.getSerializable("selectTag");
        requestData(b);


    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                ResourceItemBean.DataBean dataBean = JSON.parseObject(commonClass.getData().toString(), ResourceItemBean.DataBean.class);
                if (dataBean != null && dataBean.getList().size() != 0) {
                    tagBeanList.addAll(dataBean.getList());
                }
                mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
                    @Override
                    public boolean canScrollHorizontally() {
                        return false;
                    }

                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }

                });
                final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        observer.removeGlobalOnLayoutListener(this);
                        if (mAdapter == null) {
                            mAdapter = new SelectGoodsAdapter(mActivity, tagBeanList, mRvContent.getHeight(), mRvContent.getWidth(), num, buId,selectTag);
                        }
                        mRvContent.setAdapter(mAdapter);
                    }
                });

            } else {
                toast(commonClass.getMsg());
            }
        }
    }

    @Override
    public void onReLoad(int code) {

    }

    private void requestData(Bundle b) {
        Map<String, Object> map = new HashMap<>();
        map.put("buId", b.getString("buId")); //   业务单位ID(必填)
        map.put("page", b.getInt("pos") + ""); //
        map.put("pbUkid", b.getString("pbUkid")); //  发布ukid
        map.put("rsName", b.getString("rsName")); // 资源名称
        map.put("size", b.getInt("num") + ""); //
        map.put("sort", b.getString("sort")); //     排序(DESC,ASC)
        httpPost(ContractConstant.RESOURCEITEMS, map, 0, true, "");
    }

}
